#include "application.h"

namespace WinApp
{
	HINSTANCE Application::hInst=NULL;

	Application::Application(HINSTANCE hInstance)
	{
		hInst=hInstance;
	}

	Application::~Application(void)
	{
	}

	int Application::run()
	{
		MSG msg;
		ZeroMemory( &msg, sizeof( msg ) );

		while( msg.message!=WM_QUIT )
		{
			static DWORD hadFocus = 1;
			DWORD hasFocus = (GetWindowThreadProcessId(GetForegroundWindow(),NULL) == GetCurrentThreadId());
			if( hadFocus && !hasFocus )
			{
				SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );
			}
			else if( hasFocus && !hadFocus )
			{
				SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_NORMAL );
			}
			hadFocus = hasFocus;

			// Check the message queue
			if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE) )
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			else
			{
				if( !hasFocus )
				{
					Sleep(50);
				}
				else
				{
					render();
				}
			}

		}
		return (int)msg.wParam;
	}
}